package com.example.schoolenterprisealliance.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.schoolenterprisealliance.R;
import com.example.schoolenterprisealliance.adapter.SearchContextAdapter;
import com.example.schoolenterprisealliance.adapter.SearchHistoryAdapter;
import com.example.schoolenterprisealliance.adapter.SearchHotAdapter;
import com.example.schoolenterprisealliance.bean.Admin;
import com.example.schoolenterprisealliance.bean.SearchHot;
import com.example.schoolenterprisealliance.io.AppConfig;
import com.example.schoolenterprisealliance.service.Search;

import java.util.ArrayList;
import java.util.List;

public class SearchHotActivity extends AppCompatActivity {
    private SearchHotAdapter searchHotAdapter;
    private SearchHistoryAdapter searchHistoryAdapter;
    private SearchContextAdapter searchContextAdapter;
    private RecyclerView rv_history;
    private RecyclerView rv_hot;
    private RecyclerView rv_searchContext;
    private EditText et_search;
    private ImageView iv_search;
    private ImageView iv_search_cancel;
    private ImageView iv_cancel;
    private ImageView iv_cancel_red;
    private TextView tv_history;
    private TextView tv_cancel_history ;
    private List<Admin.DataBean> data;
    private List<Admin.DataBean> nulldata;
    List<String> historyData=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_hot);
        initView();
        //下滑上拉时消除阴影
        rv_hot.setOverScrollMode(View.OVER_SCROLL_NEVER);
        rv_history.setOverScrollMode(View.OVER_SCROLL_NEVER);
        //一个是前一个activity的退出，另一个activity的进入。
        overridePendingTransition(R.animator.into_left,R.animator.out_right);
        //图片表示隐藏；
        iv_search_cancel.setVisibility(View.INVISIBLE);
        iv_cancel_red.setVisibility(View.INVISIBLE);
        layoutManaget();
        TextChangedListener();
        cancelSearch();
        searchOutCancel();
        intoHistory();
        cancelHistory();
        showSoftInputFromWindow(et_search);
    }
    //初始化控件
    private void initView(){
        rv_history=findViewById(R.id.rv_history);
        tv_history=findViewById(R.id.tv_history);
        rv_hot=findViewById(R.id.rv_hot);
        rv_searchContext=findViewById(R.id.rv_searchContext);
        et_search=findViewById(R.id.et_search);
        iv_search=findViewById(R.id.iv_search);
        iv_search_cancel=findViewById(R.id.iv_search_cancel);
        iv_cancel=findViewById(R.id.iv_cancel);
        rv_hot=findViewById(R.id.rv_hot);
        tv_cancel_history=findViewById(R.id.tv_cancel_history);
        iv_cancel_red=findViewById(R.id.iv_cancel_red);
    }
    //布局管理器
    private void layoutManaget(){
        GridLayoutManager layoutManager1=new GridLayoutManager(this,2);
        layoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
        rv_hot.setLayoutManager(layoutManager1);
        rv_hot.setAdapter(new SearchHotAdapter(R.layout.item_hot,initHot()));

        LinearLayoutManager linearLayoutManager2=new GridLayoutManager(this,3);
        linearLayoutManager2.setOrientation(LinearLayoutManager.VERTICAL);
        rv_history.setLayoutManager(linearLayoutManager2);
        SearchHistoryAdapter searchHistoryAdapter =new SearchHistoryAdapter(R.layout.item_history,initHistory());
        rv_history.setAdapter(searchHistoryAdapter);

        LinearLayoutManager linearLayoutManager3=new LinearLayoutManager(this);
        linearLayoutManager3.setOrientation(LinearLayoutManager.VERTICAL);
        rv_searchContext.setLayoutManager(linearLayoutManager3);
        rv_searchContext.addItemDecoration(new DividerItemDecoration(SearchHotActivity.this,DividerItemDecoration.VERTICAL));
    }
    private List<SearchHot> initHot(){
        List<SearchHot> data=new ArrayList<>();
        SearchHot searchHot;
        for (int i=0;i<10;i++){
            searchHot=new SearchHot();
            searchHot.setNumber(i+1+"");
            searchHot.setName("今日热点"+(i+1));
            data.add(searchHot);
            Log.e("热门搜索",data.toString());
        }
        return data;
    }
    private List<String> initHistory(){

        for (int i=0;i<10;i++){
            historyData.add("历史搜索"+(i+1));
        }
        return historyData;
    }
    //输入框改变实时监听
    private void TextChangedListener(){
           et_search.addTextChangedListener(new TextWatcher() {
               @Override
               public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    //text  输入框中改变前的字符串信息
                   //start 输入框中改变前的字符串的起始位置
                   //count 输入框中改变前后的字符串改变数量一般为0
                   //after 输入框中改变后的字符串与起始位置的偏移量
               }
               @Override
               public void onTextChanged(CharSequence s, int start, int before, int count) {
                   //text  输入框中改变后的字符串信息
                   //start 输入框中改变后的字符串的起始位置
                   //before 输入框中改变前的字符串的位置 默认为0
                   //count 输入框中改变后的一共输入字符串的数量
                   if (s.length()!=0){
                       iv_search_cancel.setVisibility(View.VISIBLE);
                       //1, 声明Retrofit 的构建者
                       Retrofit.Builder builder = new Retrofit.Builder();
                       //2, 设置公共的地址
                       builder.baseUrl(AppConfig.BASE_URL);
                       //3, 设置解析gson
                       builder.addConverterFactory(GsonConverterFactory.create());
                       //4, 得到Retrofit 对象
                       Retrofit retrofit = builder.build();
                       //5, 得到接口
                       Search search = retrofit.create(Search.class);
                       //6, 得到Call 的方法
                       Call<Admin> call = search.getAdminData(1,s+"");
                       //7, 传入接口回调 , 获取数据
                       call.enqueue(new Callback<Admin>() {
                           @Override
                           public void onResponse(Call<Admin> call, Response<Admin> response) {
                        // 获取到的数据
                               Admin admin = response.body();
                               //数据源
                               data = admin.getData();
                               searchContextAdapter=new SearchContextAdapter(R.layout.item_searchcontext,data);
                               rv_searchContext.setAdapter(searchContextAdapter);
                               searchContextAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                   @Override
                                   public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                       Intent intent=new Intent(SearchHotActivity.this,ItemAdminActivity.class);
                                       intent.putExtra("username",data.get(position).getUsername());
                                       intent.putExtra("position",data.get(position).getPosition());
                                       intent.putExtra("context",data.get(position).getContext());
                                       intent.putExtra("certificate",data.get(position).getLogoUrl());
                                       startActivity(intent);
                                   }
                               });
                           }
                           @Override
                           public void onFailure(Call<Admin> call, Throwable t) {
                               t.printStackTrace();
                           }
                       });
                   } else {
                       nulldata=data;
                       data.clear();
                       searchContextAdapter=new SearchContextAdapter(R.layout.item_searchcontext,nulldata);
                       rv_searchContext.setAdapter(searchContextAdapter);
                       iv_search_cancel.setVisibility(View.INVISIBLE);
                   }
               }
               @Override
               public void afterTextChanged(Editable s) {
                   //edit  输入结束呈现在输入框中的信息
               }
           });
    }
    //点击输入框中的×，清除输入框中的值
    private void cancelSearch(){
        iv_search_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                et_search.setText("");
            }
        });
    }
    //点击输入框外的×返回
    private void searchOutCancel(){
        iv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iv_cancel_red.setVisibility(View.VISIBLE);
                iv_cancel.setVisibility(View.INVISIBLE);
                finish();
                //一个是前一个activity的退出，另一个activity的进入。
                overridePendingTransition(R.animator.into_right, R.animator.out_left);
                Log.e("searchOutCancel","被点击了");
            }
        });
    }
    //点击搜索图片将搜索类容加入历史搜索列表
    private void intoHistory(){
        iv_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String search=et_search.getText().toString();
                Log.e("search",search.length()+"   ");
                if (search.length()==0){
                    Toast.makeText(SearchHotActivity.this,"请输入搜索内容",Toast.LENGTH_LONG).show();
                }else {
                        if (historyData.size()==0){
                            tv_history.setHeight(40);
                            tv_history.setVisibility(View.VISIBLE);
                            tv_cancel_history.setHeight(30);
                            tv_cancel_history.setVisibility(View.VISIBLE);
                            historyData.add(search);
                            rv_history.setAdapter(new SearchHistoryAdapter(R.layout.item_history,historyData));
                        }else {
                            historyData.add(search);
                            rv_history.setAdapter(new SearchHistoryAdapter(R.layout.item_history,historyData));
                        }

                }
            }
        });
    }
    //点击清空数据并隐藏
    private void cancelHistory(){
        tv_cancel_history.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<String> data=new ArrayList<>();
                data=initHistory();
                data.clear();
                rv_history.setAdapter(new SearchHistoryAdapter(R.layout.item_history,data));
                tv_cancel_history.setHeight(0);
                tv_cancel_history.setVisibility(View.INVISIBLE);
                tv_history.setHeight(0);
                tv_history.setVisibility(View.INVISIBLE);
            }
        });
    }
    //弹出软键盘
    public void showSoftInputFromWindow(EditText editText){
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        SearchHotActivity.this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }
}
